package com.stg.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.stg.bean.Assn;
import com.stg.bean.Message;
import com.stg.bean.Student;
import com.stg.dao.StudentDao;
import com.stg.service.AssnService;
import com.stg.service.RoleService;
import com.stg.service.StudentService;
import com.stg.service.UserService;

@Service("studentService")
public class StudentServiceImpl implements StudentService{

	
	@Resource
	private StudentDao studentDao;
	
	@Resource
	private UserService userService;
	
	@Resource
	private AssnService assnService;
	
	@Resource
	private RoleService roleService;
	/**
	 * 录入学生信息
	 */
	@Override
	public Student createStudent(Student student) {
		long id = studentDao.createStudent(student);
		student.setId(id);
		
		/**
		 * 学生信息录入不能直接调用创建用户，因为无法设置用户名和密码
		 * 两个功能应该分开进行，student_id等参数应该由controller传入
		 */
		/*User user = new User();
		user.setContent(DBMsgConstant.HH_SUTDNET_CONTENT);
		user.setHeadPic(null);
		user.setstudentId(student.getStudentId());
		user.setUsername(student.getStudentId());
		user.setPassword(student.getStudentId());
		userService.createUser(user);
		System.out.println("此时的用户ID为：" + user.getId());
		userService.UserRoleInsert(user.getId(), roleService.findIDByRoleName("admin"));*/
		
		return student;
	}

	/**
	 * 通过学号获取学生信息
	 */
	@Override
	public Student findStudentById(String studentId) {
		return studentDao.findStudentById(studentId);
	}

	/**
	 * 通过学号获取所有关注社团
	 */
	@Override
	public List<Assn> findAllAssnAttentionByStudentId(String StudentId) {
		//获取学生关注的社团id
		List<Long> assnIdList = studentDao.findAllAssnAttentionIdByStudentId(StudentId);
		
		//通过社团id值获取社团信息
		List<Assn> assnList = new ArrayList<Assn>();
		for (Long assnId : assnIdList) {
			assnList.add(assnService.findAssnById(assnId));
		}
		return assnList;
	}

	/**
	 * 通过学号获取所有所在社团
	 */
	@Override
	public List<Assn> findAllAssnPendedByStudentId(String StudentId) {
		//获取学生所在的社团id
		List<Long> assnIdList = studentDao.findAllAssnPendedIdByStudentId(StudentId);
		
		//通过社团id值获取社团信息
		List<Assn> assnList = new ArrayList<Assn>();
		for (Long assnId : assnIdList) {
			assnList.add(assnService.findAssnById(assnId));
		}
		return assnList;
	}

	/**
	 * 通过学号获取所有报名社团
	 */
	@Override
	public List<Assn> findAllAssnPendingByStudentId(String StudentId) {
		//获取学生报名的社团id
		List<Long> assnIdList = studentDao.findAllAssnPendingIdByStudentId(StudentId);
		
		//通过社团id值获取社团信息
		List<Assn> assnList = new ArrayList<Assn>();
		for (Long assnId : assnIdList) {
			assnList.add(assnService.findAssnById(assnId));
		}
		return assnList;
	}

	/**
	 * 报名社团
	 */
	@Override
	public void assnSignUp(String studentId, long assnId,long departmentId) {
		studentDao.assnSignUp(studentId, assnId,departmentId);
	}

	/**
	 * 关注社团
	 */
	@Override
	public void assnAttention(String studentId, long assnId) {
		studentDao.assnAttention(studentId, assnId);
	}
	/**
	 * receive student's message
	 */
	@Override
	public List<Message> studentMessageReceive(Long receiveId){
		return studentDao.studentMessageReceive(receiveId);
	}

	@Override
	public Message studentMessageDetail(int senderId, int receiverId,int id) {
		
		return studentDao.studentMessageDetail(senderId, receiverId,id);
	}

}
